<?
include("./class/resize_image.php");

switch($act){
	case "edit":	
	Edit();
	$tpl="edit";
	$title_page = "CMS - Chỉnh sửa cấu hình";
	break;
	
	case "mail_config_edit":	
	Edit();
	$tpl="mail_config_edit";
	$title_page = "CMS - Chỉnh sửa cấu hình";
	break;
	
	case "add":	
	$tpl="edit";
	break;
	
	case "mail":	
	$tpl="mail_config";
	$title_page = "CMS - Cấu hình email";
	ShowList();
	break;
	
	case "del":
	Del();
	break;
	
	case "dellist":
	DelList();
	break;
	
	case "addsm":
	case "editsm":
	Editsm();
	break;
	
	default:
	ShowList();
	$title_page = "CMS - Cấu hình thông tin website";
	$tpl="list";
}

function ShowList()
{
	global $db,$stips,$page,$plpage,$set_per_page,$c;
	$set_per_page=20; 
	$sql="select * from infos where isallowed = 1 order by alias asc, id asc ";
	$c = $db->numRows($db->query($sql));
	$plpage = plpage($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);	
	$stips = $db->getAll($sqlstmt);
}

function Edit()
{
	global $db, $info;
	$id=$_GET["id"];
	$sql = "select * from infos where id=$id";
	$info = $db->getRow($sql);
	print_r($info);
}
/*
function DelList()
{
	global $db;
	$id=$_POST["iddel"];		
	for($i=0;$i<count($id);$i++){		
		$sqlstmt="select img from `infos` where id=".$id[$i];
		$r = $db->getRow($sqlstmt);
		if(file_exists($r['img'])) unlink($r['img']);
		
		$sql="delete from infos  where id=".$id[$i];
		$db->query($sql);		
	}
	$msg="Delete successfully";		
	$page="admin-pro.html?do=infos";
	page_transfer2($page);
}*/
/*
function Del()
{
	global $db;
	$id=$_GET["id"];

	$sqlstmt="select img from `infos` where id=$id";
	$r = $db->getRow($sqlstmt);
	if(file_exists($r['img'])) unlink($r['img']);		
	$sql="delete from infos  where id=".$id;
	$db->query($sql);	
		
	$msg="Delete successfully";		
	$page="admin-pro.html?do=infos";
	page_transfer2($page);
}
*/
function Editsm()
{
	global $db,$act;
	$arr['name_vn']=$_POST["name_vn"]?$_POST["name_vn"]:'';				
	$arr['plain_text_vn']=$_POST["plain_text_vn"]?$_POST["plain_text_vn"]:'';
	$arr['content_vn']=$_POST["content_vn"]?$_POST["content_vn"]:'';
	$arr['alias']=$_POST["alias"]?$_POST["alias"]:'';
	
	if(isset($_FILES['img']['name'] ) && $_FILES['img']['size']>0){
		$img = $_FILES['img']['name'];
		$start = strpos($img,".");
		$type = substr($img,$start,strlen($img));
		$filename = 'img'.time().$type;
		$filename = strtolower($filename);
		copy($_FILES['img']['tmp_name'], "./upload/images/" . $filename);
		$arr['img'] = "upload/images/" . $filename;		 
	}

	if ($act=="addsm")
	{			
		vaInsert('infos',$arr);
		$msg="Add successfully";
	}
	else
	{
		$id=$_POST['id'];
		if($id == 64 || $id == 67  || $id == 68) $arr['content_vn'] = isset($_POST["flag"])?$_POST["flag"]:0;
		else if($id == 65)
		{
			$image = new SimpleImage();
		   	$image->load("./upload/images/" . $filename);
		   	$image->resize(16, 16);
		   	$image->save("./upload/images/" . $filename);	
		}
		
		if (isset($arr['img'])){
			$sqlstmt="select img from `infos` where id=$id";
			$r = $db->getRow($sqlstmt);
			if(file_exists($r['img'])) unlink($r['img']);
		}			
		vaUpdate('infos',$arr,' id='.$id);	
		$msg="Edit successfully";		
	}
	
	$id = isset($_POST['id'])?$_POST['id']:0;
	if($id == 59 || $id == 58 || $id == 60 || $id == 57 || $id == 15)
		$page="admin-pro.html?do=infos&act=mail";
	else
		$page="admin-pro.html?do=infos";
		
	$_SESSION['mess'] = $msg;	
	page_transfer2($page);
}
?>